import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import getDisabledIndex from "./getDisabledIndex";
import updateSubButtonStatus from "./updateSubButtonStatus";

export default function addLine() {
    const {areaCode} = this.state;
    const {selectedPsn, orgVal, editTable, disabledAllArea, func_type} = this.props;
    const allRows = editTable.getAllRows(areaCode);
    let postData = {
        areaCode,
        func_type,
        pk_org: orgVal.refpk,
        pk_psnjob: selectedPsn
    };

    return proFetch({
        url: '/nccloud/hrtrn/pub/RecordsAddLineAction.do',
        body: postData,
    })
        .then((res) => {
            if (res.success) {
                editTable.addRow(areaCode, undefined, true, res.data[areaCode].rows[0].values);
                const disabledIndex = getDisabledIndex(-1, allRows.length);
                disabledIndex && editTable.setEditableRowByIndex(areaCode, disabledIndex, false);
                this.hideAction();
                disabledAllArea(true);
                updateSubButtonStatus.call(this, true);
                this.props.button.setButtonDisabled({
                    'delete': true,
                    'edit': true,
                    'change': true
                })
            }
        });
}
